package com.liuqi.openai.data.document;

import static com.liuqi.openai.util.ValidationUtil.ensureNotBlank;
import static com.liuqi.openai.util.ValidationUtil.ensureNotNull;

/**
 * 对应于单个文件内容的非结构化文本
 *
 * @see DocumentParser
 *
 * @author liuqi
 * @date 2025/8/7
 **/
public class Document {

    public static final String DOCUMENT_TYPE = "document_type";
    public static final String FILE_NAME = "file_name";
    public static final String ABSOLUTE_DIRECTORY_PATH = "absolute_directory_path";
    public static final String URL = "url";

    public final static String SUBJECT = "subject";

    /**
     * 文档
     */
    private final String text;

    /**
     * 文档元数据
     */
    private final Metadata metadata;

    public Document(String text, Metadata metadata) {
        this.text = ensureNotBlank(text, "text");
        this.metadata = ensureNotNull(metadata, "metadata");
    }

    public String text() {
        return text;
    }

    public Metadata metadata() {
        return metadata;
    }

    public String metadata(String key) {
        return metadata.get(key);
    }

    @Override
    public String toString() {
        return "Document {" +
                " text = " + text +
                " metadata = " + metadata.asMap() +
                " }";
    }

    public static Document from(String text) {
        return new Document(text, new Metadata());
    }

    public static Document from(String text, Metadata metadata) {
        return new Document(text, metadata);
    }

    public static Document document(String text) {
        return from(text);
    }

    public static Document document(String text, Metadata metadata) {
        return from(text, metadata);
    }

}
